<?php
	Header('Cache-Control: no-cache');
	Header('Pragma: no-cache');
	Header('X-UA-Compatible: IE=9');
	
	session_start();
?>
<!DOCTYPE html>
<html>
  <head>
    <title>Telefonliste</title>
	<meta name="viewport"
        content="width=device-width, initial-scale=1.0, user-scalable=no">
    <meta charset="UTF-8">
	<link rel="icon" href="icons/favicon.ico" type="image/x-icon">
    <link rel="stylesheet" type="text/css" href="css/style.css">
  </head>
  <body>
	<?php 
		require_once 'navbar.php';
		
		if (!isset($_SESSION['userid']))
		{
			$db->showMsg(false,"");
			exit;
		}
		
		$listid = 1;
		if (isset($_GET['listid']))
		{
			$listid = $_GET['listid'];
		}
		
		if (isset($_GET['delete']))
		{
			$db->deletePhonelistEntry($_GET['delete']);
		}
		else if (isset($_GET['newentry']))
		{
			$db->insertPhonelistEntry($listid);
		}
		
		$res = $db->getPhonelists();
	?>
	
	<form name="listid" action="phonelist.php">
		<div class="phonelists">
			<b>Liste:</b>
			<select name="listid" size="1" onchange="this.form.submit();">
	<?php
		while ($row = $res->fetch_assoc())
		{
			echo '<option value="'.$row['id'].'"';
			if ($row['id'] == $listid)
			{
				echo ' selected="selected"';
			}
			echo '>'.$row['name'].'</option>'."\n";
		}
	?>
			</select>
		</div>
	</form>
	
	<table>
		<tr class="phonelist">
			<th class="lefty">Name</th>
			<th class="lefty">Vorname</th>
			<th class="lefty">Telefon</th>
			<th class="lefty">Mobil</th>
			<th class="lefty">Aktion</th>
		</tr>
		
	<?php
		$res = $db->getPhonelist($listid);
		$colorRow = true;
		while ($row = $res->fetch_assoc())
		{
			echo '<tr class="phonelist';
			if ($colorRow)
				echo "2";

			echo '">'."\n"
				. "<td>".$row['name']."</td>\n"
				. "<td>".$row['firstname']."</td>\n"
				. "<td>".$row['phone']."</td>\n"
				. "<td>".$row['mobile']."</td>\n"
				. "<td>"
				. '<input type="button" value="Bearbeiten" name="edit" '
				. 'onclick="window.location=\'editphonelistentry.php?id='.$row['id'].'\';" />'."\n"
				. '<input type="button" value="L&ouml;schen" name="delete" '
				. 'onclick="if (confirm(\'Eintrag wirklich l&ouml;schen?\')) '
				. 'window.location.search=\'?listid='.$listid.'&amp;delete='.$row['id'].'\';" />'."\n"
				. "</td>\n"
				. "</tr>\n";
				
				$colorRow = !$colorRow;
		}
	?>
		
	</table>

	<div class="login">
		<form name="newEntry" action="phonelist.php">
			<input type="hidden" name="listid" value="<?php echo $listid; ?>" />
			<input type="hidden" name="newentry" value="1" />
			<input type="submit" value="Neuen Eintrag anlegen" />
		</form>
	</div>
  </body>
</html>
